﻿using Micro.Extensions.Refit;
using Microsoft.Extensions.DependencyInjection;
using Refit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class ServiceCollectionExtensions
    {
        internal static List<Type> RefitInterfacServices = new List<Type>();
        internal static List<RefitOption> RefitOptionServices = new List<RefitOption>();
        public static IServiceCollection AddRefitServices(this IServiceCollection services, params Type[]? interfacTypes)
        {
            if (interfacTypes != null && interfacTypes.Any())
            {
                foreach (var type in interfacTypes)
                    if (!RefitInterfacServices.Contains(type))
                        RefitInterfacServices.Add(type);
            }
            return services;
        }
        //internal static IServiceCollection AddRefitServices(this IServiceCollection services, RefitOption refitOption)
        //{
        //    if (refitOption != null && !RefitOptionServices.Contains(refitOption))
        //        RefitOptionServices.Add(refitOption);
        //    return services;
        //}

        public static IServiceCollection AddRefitServices(this IServiceCollection services, RefitOption refitOption, params Type[]? interfacTypes)
        {
            services.AddRefitServices(interfacTypes);
            if (refitOption != null && !RefitOptionServices.Contains(refitOption))
                RefitOptionServices.Add(refitOption);
            return services;
        }


        internal static IServiceCollection ExecAddRefitServices(this IServiceCollection services)
        {
            foreach (var refitOption in RefitOptionServices)
            {

                if (refitOption != null && refitOption.Enabled && refitOption.RefitUrls != null)
                {
                    foreach (var refit in refitOption.RefitUrls)
                    {
                        if (refit.Url.NotNull() && refit.Interfacs != null)
                        {
                            int timeout = refit.Timeout > 0 ? refit.Timeout : refitOption.Timeout;
                            foreach (var f in refit.Interfacs)
                            {
                                var type = GetType(f);
                                if (type == null)
                                    continue;
                                //注册refit 服务
                                services.AddRefitClient(type)
                                    .ConfigureHttpClient(c =>
                                    {
                                        c.BaseAddress = new Uri(refit.Url);
                                        c.Timeout = TimeSpan.FromMilliseconds(timeout);
                                    });
                            }
                        }

                    }
                }
            }
            return services;
        }


        private static Type GetType(string typeName)
        {
            var type = RefitInterfacServices.FirstOrDefault(r => r.Name.EqualsIgnoreCase(typeName));
            return type;
        }
    }
}
